package cc.rengu.igas.bomp.core.service.trans;

import cc.rengu.igas.bomp.common.dao.TermInfoMapper;
import cc.rengu.igas.bomp.common.dao.impl.TermInfoMapperImpl;
import cc.rengu.igas.bomp.common.entity.TermInfo;
import cc.rengu.igas.bomp.common.enums.RespCodeEnum;
import cc.rengu.igas.bomp.core.service.base.ManageService;
import cc.rengu.igas.bomp.facade.request.RegisterTermRequest;
import cc.rengu.igas.bomp.facade.response.RegisterTermResponse;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.BizResponse;
import cc.rengu.oltp.utility.util.ConvertUtil;
import cc.rengu.oltp.utility.util.StringUtil;

/**
 * 注册终端设备
 * Created by moxm
 */
public class RegisterTermService extends ManageService {
    @Override
    protected Object bizBeforeCust() throws Exception {
        RegisterTermRequest registerTermRequest = new RegisterTermRequest();
        ConvertUtil.convertOutput(registerTermRequest);
        return registerTermRequest;
    }

    @Override
    protected BizResponse bizProcessCust(Object request) throws Exception {
        RegisterTermRequest registerTermRequest = (RegisterTermRequest) request;
        RegisterTermResponse registerTermResponse = new RegisterTermResponse();
        String instId = registerTermRequest.getHeader().getInstId();
        String userId = registerTermRequest.getUserId();
        String mposInstNo = registerTermRequest.getMposInstNo();
        String termNo = registerTermRequest.getPosInstNo();
        /*必填性校验*/
        if (StringUtil.isEmptyOrNull(userId) || StringUtil.isEmptyOrNull(mposInstNo) || StringUtil.isEmptyOrNull(termNo)) {
            rglog.error("注册终端设备交易必填性校验失败！");
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        }
        /*根据终端号查询终端序列号*/
        TermInfoMapper termInfoMapper = new TermInfoMapperImpl();
        TermInfo termInfo = termInfoMapper.selectBompTermInfoByTermNo(instId, termNo, "01");
        if (null == termInfo) {
            rglog.error("注册终端设备处理失败，查询终端设备信息为空！instId:{}, mposInstNo:{},  termNo:{}", instId, mposInstNo, termNo);
            throw new BizException(RespCodeEnum.QUERY_TERMINFO_ERROR.getRespCode(), RespCodeEnum.QUERY_TERMINFO_ERROR.getRespDesc());
        }
        String id = termInfo.getId();
        rglog.info("调加密机工具注册终端设备上送字段,instId={},mposInstNo={},id={}", instId, mposInstNo, id);
        /* 响应给请求方 */
        BizResponse bizResponse = new BizResponse();
        bizResponse.setRspSysId(AppParamConstant.SYS_ID);
        bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
        bizResponse.setResult(registerTermResponse);
        return bizResponse;
    }

    @Override
    protected void bizAfterCust(Object response) throws Exception {
        BizResponse bizResponse = (BizResponse) response;
        RegisterTermResponse registerTermResponse = (RegisterTermResponse) bizResponse.getResult();
        ConvertUtil.convertInput(registerTermResponse);
    }
}
